package com.r2coding.system.exception;

import com.r2coding.common.enums.ResultEnum;
import com.r2coding.common.exception.BizCode;
import com.r2coding.common.exception.ServiceException;
import com.r2coding.common.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author:98k灬
 * @since:2022/04/05 18:57
 **/
@RestControllerAdvice
@Slf4j
public class GlobalException {
    @ExceptionHandler(ServiceException.class)
    public Result serviceException(ServiceException e) {
        log.error("serviceException: {}", e.getMessage());
        log.error("serviceException: {}", e.getCode());
        return Result.build(e.getCode(), e.getMessage());
    }

    // 权限校验不通过异常
    @ExceptionHandler(AccessDeniedException.class)
    public Result handleAccessDeniedException(Exception e) {
        log.error("error: {}", e.getMessage());
        return Result.build(ResultEnum.MENU_NO);
    }

    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        log.error("error: {}", e.getMessage());
        return Result.build(BizCode.UNKNOW_EXCEPTION.getCode(), BizCode.UNKNOW_EXCEPTION.getMsg());
    }
}
